/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DatabazoveOkno;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.AbstractTableModel;
import java.lang.Integer;
import java.sql.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

/**
 *
 * @author Riso-Desktop
 */
public class Tabulka extends AbstractTableModel {

    int rowcount;
    int columncount;
    String nazovtabulky;
    ResultSet rs;
    Statement st;
    Object udaje[][] = null;
    ResultSetMetaData rsmd;
    boolean uprava = false; //poznačiť či je tabuľka používaná pre úpravu
    String sql;

    public Tabulka(Statement st, String nazovtabulky) {
        this.st = st;
        this.nazovtabulky = nazovtabulky;
        nacitajRS();
    }

    public Tabulka(Statement st, String nazovtabulky, String sql) {
        this.st = st;
        this.nazovtabulky = nazovtabulky;
        nacitajRS(sql);
    }

    public Tabulka(Statement st, String nazovtabulky, String sql, boolean uprava) {
        this.st = st;
        this.nazovtabulky = nazovtabulky;
        this.uprava = uprava;
        nacitajRS(sql);
    }

    private void nacitajRS() {
        try {
            rs = st.executeQuery("Select * from " + nazovtabulky);
            rsmd = rs.getMetaData();
            rs.last();
            rowcount = rs.getRow();
            rs.beforeFirst();
            columncount = rsmd.getColumnCount();
            udaje = new Object[rowcount][columncount];
            RsToData(rs);
            fireTableStructureChanged();
        } catch (SQLException ex) {
            Logger.getLogger("mojlog").log(Level.WARNING, ex.toString(), ex);
        }
    }

    private void nacitajRS(String sql) {
        try {
            this.sql = sql;
            rs = st.executeQuery(sql);
            rsmd = rs.getMetaData();
            rs.last();
            rowcount = rs.getRow();
            rs.beforeFirst();
            columncount = rsmd.getColumnCount();
            udaje = new Object[rowcount][columncount];
            RsToData(rs);
            fireTableStructureChanged();
        } catch (SQLException ex) {
            Logger.getLogger("mojlog").log(Level.WARNING, ex.toString(), ex);
        }
    }

    private void RsToData(ResultSet res) {
        try {

            for (int i = 0; i < rowcount; i++) {
                res.absolute(i + 1);
                for (int j = 0; j < columncount; j++) {
                    udaje[i][j] = res.getObject(j + 1);
                }
            }
        } catch (Exception ex) {
            Logger.getLogger("mojlog").log(Level.WARNING, ex.toString(), ex);
        }
    }

    @Override
    public String getColumnName(int column) {
        String col = null;
        try {
            col = rsmd.getColumnName(column + 1).toString();
        } catch (SQLException ex) {
        }
        return col;
    }
    
    public void reload(){
        if(sql!=null){
            nacitajRS(sql);
        }else{
            nacitajRS();
        }
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        if (uprava) {
//            try {
//                if (rsmd.isAutoIncrement(columnIndex + 1)) {  //ak je stĺpec nastavený na autoincrement
//                    return false;
//                } else {
//                    return true;
//                }
//            } catch (SQLException ex) {
//                Logger.getLogger("mojlog").log(Level.WARNING, ex.toString(), ex);
//            }
            return true;
        }
        return false;
    }

    @Override
    public int getRowCount() {
        return rowcount;
    }

    @Override
    public int getColumnCount() {
        return columncount;
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        Object obj = this.getValueAt(0, columnIndex);
        return (obj == null ? Object.class : obj.getClass());
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Object o = null;
        try {
            if(udaje.length <=0){
                return o;
            }
            o = udaje[rowIndex][columnIndex];
            if (o != null&&o.getClass() == java.sql.Timestamp.class) {
                Timestamp t = (Timestamp)o;
                o = t.toString();
            }
        } catch (Exception ex) {
            Logger.getLogger("mojlog").log(Level.WARNING, ex.toString(), ex);
        }
        return o;
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        udaje[rowIndex][columnIndex] = aValue;
    }

    public void update(String IDstlpec) {
        StringBuilder sb = new StringBuilder();
        int stlpcebezzapisu = 1;
        boolean preskok = false;
        try {

            for (int i = 0; i < rowcount; i++) {
                stlpcebezzapisu = 1;
                sb = new StringBuilder();
                for (int j = 0; j < columncount; j++) {
                    preskok = false;


                    sb.append(getColumnName(j));
                    sb.append(" = ");
                    if (getColumnClass(j) == Integer.class) {
                        sb.append(udaje[i][j] + " ");
                    } else {
                        sb.append("'" + udaje[i][j] + "' ");

                    }
                    if (j != columncount - 1) {
                        sb.append(", ");
                    }
                }
                System.out.println("update " + nazovtabulky + " set " + sb.toString() + " where " + IDstlpec + " = " + udaje[i][findColumn(IDstlpec)]);
                st.executeUpdate("update " + nazovtabulky + " set " + sb.toString() + " where " + IDstlpec + " = " + udaje[i][findColumn(IDstlpec)]);
            }
            fireTableDataChanged();
        } catch (Exception ex) {
            Logger.getLogger("mojlog").log(Level.WARNING, ex.toString(), ex);
        }
    }
}
